using System;
using System.Collections.Generic;
using Jalal.Library.DataStructure;
using BCGC.BLL.Helper;
using BCGC.DAL;
using System.Data;
using Jalal.Library.Data;
using Jalal.Library.Utility;

namespace BCGC.BLL.MarketPlace
{

    public class Market
    {

        private int itemID;
        public int ItemID
        {
            get
            {
                return itemID;
            }
        }

        private int userID;
        public int UserID
        {
            get
            {
                return userID;
            }
            set
            {
                userID = value;
            }
        }

        

        private String title;
        public String Title
        {
            get
            {
                return title;
            }
            set
            {
                title = value;
            }
        }

        private String marketContent;
        public String MarketContent
        {
            get
            {
                return marketContent;
            }
            set
            {
                marketContent = value;
            }
        }

        private DateTime date;
        public DateTime Date
        {
            get
            {
                return date;
            }
            set
            {
                date = value;
            }
        }

        private bool showContactInfo;
        public bool ShowContactInfo
        {
            get
            {
                return showContactInfo;
            }
            set
            {
                showContactInfo = value;
            }
        }

        private string contactInfo;
        public string ContactInfo
        {
            get { return contactInfo; }
            set { contactInfo = value; }
        }

        private bool isActive;
        public bool IsActive
        {
            get
            {
                return isActive;
            }
            set
            {
                isActive = value;
            }
        }

        private String adminComment;
        public String AdminComment
        {
            get
            {
                return adminComment;
            }
            set
            {
                adminComment = value;
            }
        }

        public string MarketStatus
        {
            get
            {
                if (IsActive)
                {
                    return SiteConstants.DefaultMarketOpenImage;
                }
                else
                {
                    return SiteConstants.DefaultMarketClosedImage;
                }
            }
        }
        public Market()
        {
        }

        public Market(int userID, String title, String marketContent, DateTime date, bool showContactInfo, bool isActive, string adminComment,string contactInformation)
        {
            UserID = userID;
            Title = title;
            MarketContent = marketContent;
            Date = date;
            ShowContactInfo = showContactInfo;
            IsActive = isActive;
            AdminComment = adminComment;
            ContactInfo = contactInformation;
        }

        public Market(DataRow dataRow)
        {
            if (dataRow != null)
            {

                itemID = (int)CommonUtility.FilterNull(dataRow["ItemID"], typeof(int)); 
                userID = (int)CommonUtility.FilterNull(dataRow["UserID"], typeof(int)); 
                title = (string)CommonUtility.FilterNull(dataRow["Title"], typeof(string));
                marketContent = (string)CommonUtility.FilterNull(dataRow["MarketContent"], typeof(string));
                date = (DateTime)CommonUtility.FilterNull(dataRow["Date"], typeof(DateTime)); 
                showContactInfo = (bool)CommonUtility.FilterNull(dataRow["ShowContactInfo"], typeof(bool));
                isActive = (bool)CommonUtility.FilterNull(dataRow["IsActive"], typeof(bool));
                adminComment = (string)CommonUtility.FilterNull(dataRow["AdminComment"], typeof(string));
                contactInfo = (string)CommonUtility.FilterNull(dataRow["ContactInfo"], typeof(string));

            }
            
        }
        
        private static ReadWriteCustomCollection<Market> GetCollection(DataRowCollection rowCollection)
        {
            ReadWriteCustomCollection<Market> collection = new ReadWriteCustomCollection<Market>();

            foreach (DataRow dr in rowCollection)
            {
                collection.Add(new Market(dr));
            }
            return collection;
        }
        public static ReadWriteCustomCollection<Market> GetAllMarketListing(string userName, DateTime? dtFrom, DateTime? dtTo, bool? isActive, int currentPageIndex, int pageSize)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable result = MarketPlaceData.GetAllMarketListing(userName,dtFrom,dtTo,isActive,currentPageIndex,pageSize);
            DataFactory.GetDataMiner().CommitTransaction();

            if (result.Rows.Count > 0)
                return GetCollection(result.Rows);
            else
                return null;
        }
        public static ReadWriteCustomCollection<Market> GetAllMarketListing()
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable result = MarketPlaceData.GetAllMarketListing();
            DataFactory.GetDataMiner().CommitTransaction();

            if (result.Rows.Count > 0)
                return GetCollection(result.Rows);
            else
                return null;
        }

        public static Market GetByID(int id)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable result = MarketPlaceData.GetMarketByID(id);
            DataFactory.GetDataMiner().CommitTransaction();

            if (result.Rows.Count > 0)
                return new Market(result.Rows[0]);
            else
                return null;
        }

        private int Create()
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            int result = MarketPlaceData.CreateMarketPlace(this.UserID,this.Title,this.MarketContent,this.Date,this.ShowContactInfo,this.IsActive,this.AdminComment,this.ContactInfo);
            DataFactory.GetDataMiner().CommitTransaction();
            return result;
        }

        private bool Update()
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            bool result=MarketPlaceData.UpdateMarket(this.ItemID,this.IsActive,this.AdminComment,this.MarketContent,this.ShowContactInfo,this.ContactInfo);
            DataFactory.GetDataMiner().CommitTransaction();
            return result;

        }

        public bool Save()
        {
            if (this.ItemID != 0)
            {
                return Update();
            }
            else
            {
                int result=Create();
                if (result > 0)
                {
                    this.itemID = result;
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public static bool Delete(int id)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            bool result = MarketPlaceData.DeleteMarketPlace(id);
            DataFactory.GetDataMiner().CommitTransaction();
            return result;
        }

    }

}
